package com.wuxin.controller;


import com.wuxin.result.Result;
import com.wuxin.service.MsmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @program: jinrongxiangmu
 * @ClassName ApiSmsController
 * @description:
 * @author: 李尧坤
 * @create: 2025−01-16 19:49
 * @Version 1.0
 **/

@Api(tags = "短信管理")
@RestController
@RequestMapping("/api/sms")
public class ApiSmsController {

    @Autowired
    private StringRedisTemplate  stringRedisTemplate;

    @Autowired
    private MsmService msmService;


    // http://localhost/api/sms/send/17660236270



    @ApiOperation("获取验证码功能")
    @GetMapping("/send/{mobile}")
    public Result send(
            @ApiParam(value = "手机号",required = true)
            @PathVariable("mobile") String mobile
    ) {

        //最大值
        int max = 999999;
        //最小值
        int min = 100000;

        int cardNum = (int)(Math.random()*(max-min)+min);

        String code = String.valueOf(cardNum);//随机6位数

        //String  code = "369156";//写死

        msmService.send(mobile,code);
        stringRedisTemplate.opsForValue().set("duanxin:" +mobile,code,2, TimeUnit.MINUTES);
        return Result.ok().message("短信发送成功");
    }
}
